package com.deuwise.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.deuwise.common.utils.BitStatesUtils;
import com.deuwise.common.utils.R;
import com.deuwise.common.utils.Result;
import com.deuwise.system.entity.ConfigDo;
import com.deuwise.system.entity.PassWordConfig;
import com.deuwise.system.entity.from.PassConfigForm;
import com.deuwise.system.service.ConfigDoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Copyright
 *
 * @author chenwenchao
 * @date 2019/3/19
 * Description:
 */
@Api(tags = "系统配置模块", description = "系统配置模块")
@RequestMapping("api/configDo")
@RestController()
public class ConfigDoController {

    @Autowired
    private ConfigDoService configDoService;
    @ApiOperation("密码配置")
    @GetMapping("/getPassWordConfig")
    @RequiresPermissions("sys:config:info")
    public Result<PassConfigForm> getPassWordConfig(){

        PassConfigForm passConfigForm=configDoService.getPassWordConfig();
        if(passConfigForm!=null){
            return  Result.ok(passConfigForm);

        }else{
            return Result.build(500,"获取失败");
        }
    }

    @ApiOperation("获取配置项根据code")
    @GetMapping("/getConfigByCode")
    public Result<ConfigDo> getPassWordConfig(String code){

        Wrapper<ConfigDo> wrapper=new EntityWrapper<>();
        wrapper.eq("code",code);
        ConfigDo configDo=configDoService.selectOne(wrapper);
        if(configDo!=null){
            return  Result.ok(configDo);

        }else{
            return Result.build(500,"获取失败");
        }
    }

    @ApiOperation("修改密码配置")
    @PutMapping("/updatePassWordConfig")
    @RequiresPermissions("sys:config:update")
    public R updatePassWordConifg(@RequestBody PassConfigForm passConfigForm){
        Integer result=configDoService.updatePassWordConfig(passConfigForm);
        if(result>0) {
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }

    }
    @ApiOperation("获取日志配置项")
    @GetMapping("/getLogConfig")
    @RequiresPermissions("sys:config:info")
    public Result<List<ConfigDo>> getLogConfig(){
        Wrapper<ConfigDo> configDoWrapper=new EntityWrapper<>();
        configDoWrapper.eq("group_name","日志");
        List<ConfigDo> configDoList= configDoService.selectList(configDoWrapper);
        return  Result.ok(configDoList);
    }
    @ApiOperation("修改配置项")
    @PutMapping()
    @RequiresPermissions("sys:config:update")
    public R updateConfig(@RequestBody ConfigDo configDo){

        boolean result= configDoService.updateById(configDo);
        if(result){
            return  R.ok();
        }else{
            return  R.error("修改失败");
        }
    }


}
